//
//  MTSearchFriendResultHeaderCell.swift
//  MT
//
//  Created by ooops on 2020/5/11.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import Kingfisher

class MTSearchFriendResultHeaderCell: UITableViewCell {

    
//    var user: JMSGUser! {
//        didSet {
//            if let avator = user.avatar {
//                self.userIconView.kf.setImage(with: URL(string: avator), placeholder: UIImage(named: "placeholder-square"))
//            }
//
//            self.nickNameLabel.text = user.nickname
//            //self.userAreaLabel.text = user.address
//        }
//    }
    
    var userModel: MTSearchUserInfoModel? {
        didSet {
            if let m = userModel {
//                if let avator = m.headerUrl {
//                    self.userIconView.kf.setImage(with: URL(string: avator), placeholder: UIImage(named: defaultPlaceholderIcon60x60))
//                }
                self.userIconView.mtSetImage(userModel?.headerUrl, nil)
                
                if let nickName = m.nickname {
                    nickNameLabel.text = nickName
                }
                
                if let mid = m.id {
                    mIDLabel.text = "觅颜ID:\(mid)"
                }
                
                if let a = m.age, let area = m.area {
                    userAreaLabel.text = "\(a)岁 " + area
                }
                
                if let g = m.sex {
                    userGenderImageView.image = UIImage(named: g == 1 ? "sex-woman-search" : "sex-man-search")
                }
            }
        }
    }
    
    class func reuseIdentity() -> String {
        return "MTSearchFriendResultHeaderCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        selectionStyle = .none
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var userIconView: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 40
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var nickNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(18)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var mIDLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(10)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var userAreaLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var userGenderImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.sizeToFit()
        return imageV
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xf4f4f4)
        return v
    }()
    
//    func showLine(long: Bool) {
//        if long {
//            lineView.snp.updateConstraints { (make) in
//                make.left.equalToSuperview().offset(20)
//            }
//        } else {
//            lineView.snp.updateConstraints { (make) in
//                make.left.equalToSuperview().offset(0)
//            }
//        }
//    }
    
    private func setupUI() {
        contentView.addSubview(userIconView)
        contentView.addSubview(nickNameLabel)
        contentView.addSubview(mIDLabel)
        contentView.addSubview(userGenderImageView)
        contentView.addSubview(userAreaLabel)
        contentView.addSubview(lineView)
    }
    
    private func makeConstraint() {
        userIconView.snp.makeConstraints { (make) in
            make.top.left.equalTo(20)
            make.width.height.equalTo(80)
        }
        
        nickNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIconView)
            make.left.equalTo(userIconView.snp.right).offset(10)
        }
        
        mIDLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nickNameLabel)
            make.top.equalTo(nickNameLabel.snp.bottom)
        }
        
        userAreaLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nickNameLabel)
            make.bottom.equalTo(userIconView.snp.bottom)
        }
        
        userGenderImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(userAreaLabel)
            make.left.equalTo(userAreaLabel.snp.right).offset(7)
            make.width.equalTo(27)
            make.height.equalTo(20)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(0)
            make.height.equalTo(1)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
